package com.spring.sunflower.conversation;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.spring.sunflower.R;
import com.spring.sunflower.base.AppBaseFragment;
import com.spring.sunflower.bottompanel.EmotionComplateFragment;
import com.spring.sunflower.bottompanel.EmotionMainFragment;
import com.spring.sunflower.bottompanel.EmotionUtils;
import com.spring.sunflower.bottompanel.EmotionVerticalFragment;
import com.spring.sunflower.bottompanel.FragmentFactory;
import com.spring.sunflower.bottompanel.GlobalOnItemClickManagerUtils;
import com.spring.sunflower.bottompanel.HorizontalRecyclerviewAdapter;
import com.spring.sunflower.bottompanel.ImageModel;
import com.spring.sunflower.bottompanel.NoHorizontalScrollerVPAdapter;
import com.spring.sunflower.util.SPUtils;
import com.spring.sunflower.widget.NoSmoothViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kevin on 2021/7/30<br/>
 * <p>
 * Describe:<br/>
 */
public class EmotionSubFragment extends AppBaseFragment {

    //当前被选中底部tab
    private static final String CURRENT_POSITION_FLAG = "CURRENT_POSITION_FLAG";
    private int CurrentPosition = 0;
    //底部水平tab
    private RecyclerView recyclerview_horizontal;
    private HorizontalRecyclerviewAdapter horizontalRecyclerviewAdapter;
    //不可横向滚动的ViewPager
    private NoSmoothViewPager viewPager;

    //是否绑定当前Bar的编辑框,默认true,即绑定。
    //false,则表示绑定contentView,此时外部提供的contentView必定也是EditText
    private boolean isBindToBarEditText = true;

    //是否隐藏bar上的编辑框和发生按钮,默认不隐藏
    private boolean isHidenBarEditTextAndBtn = false;

    List<Fragment> fragments = new ArrayList<>();

    /**
     * 创建与Fragment对象关联的View视图时调用
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_emotion_sub, container, false);
        initView(rootView);
        initListener();
        //创建全局监听
        GlobalOnItemClickManagerUtils globalOnItemClickManager = GlobalOnItemClickManagerUtils.getInstance(getActivity());

        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        initDatas();
    }
    /**
     * 绑定内容view
     *
     * @param contentView
     * @return
     */

    /**
     * 初始化view控件
     */
    protected void initView(View rootView) {
        viewPager = (NoSmoothViewPager) rootView.findViewById(R.id.vp_emotionview_layout);
        recyclerview_horizontal = (RecyclerView) rootView.findViewById(R.id.recyclerview_horizontal);

    }

    /**
     * 初始化监听器
     */
    protected void initListener() {

    }


    /**
     * 数据操作,这里是测试数据，请自行更换数据
     */
    protected void initDatas() {
        fragments.clear();
        replaceFragment();
        List<ImageModel> list = new ArrayList<>();
        for (int i = 0; i < fragments.size(); i++) {
            printD("fragment.size="+fragments.size());
            if (i == 0) {
                ImageModel model1 = new ImageModel();
                model1.icon = getResources().getDrawable(R.drawable.ic_more_emoji);
                model1.flag = "经典笑脸";
                model1.isSelected = true;
                list.add(model1);
            } else {
                ImageModel model = new ImageModel();
                model.icon = getResources().getDrawable(R.drawable.ic_add);
                model.flag = "其他笑脸" + i;
                model.isSelected = false;
                list.add(model);
            }
        }

        //记录底部默认选中第一个
        CurrentPosition = 0;
        SPUtils.setSP(CURRENT_POSITION_FLAG, getActivity(), CurrentPosition);
//        SharedPreferencedUtils.setInteger(getActivity(), CURRENT_POSITION_FLAG, CurrentPosition);

        //底部tab
        horizontalRecyclerviewAdapter = new HorizontalRecyclerviewAdapter(getActivity(), list);
        recyclerview_horizontal.setHasFixedSize(true);//使RecyclerView保持固定的大小,这样会提高RecyclerView的性能
        recyclerview_horizontal.setAdapter(horizontalRecyclerviewAdapter);
        recyclerview_horizontal.setLayoutManager(new GridLayoutManager(getActivity(), 1, GridLayoutManager.HORIZONTAL, false));
        //初始化recyclerview_horizontal监听器
        horizontalRecyclerviewAdapter.setOnClickItemListener(new HorizontalRecyclerviewAdapter.OnClickItemListener() {
            @Override
            public void onItemClick(View view, int position, List<ImageModel> datas) {
                //获取先前被点击tab
                int oldPosition = SPUtils.getIntSP(CURRENT_POSITION_FLAG, getActivity());
                //修改背景颜色的标记
                datas.get(oldPosition).isSelected = false;
                //记录当前被选中tab下标
                CurrentPosition = position;
                datas.get(CurrentPosition).isSelected = true;
//                SharedPreferencedUtils.setInteger(getActivity(), CURRENT_POSITION_FLAG, CurrentPosition);
                SPUtils.setSP(CURRENT_POSITION_FLAG, getActivity(), CurrentPosition);
                //通知更新，这里我们选择性更新就行了
                horizontalRecyclerviewAdapter.notifyItemChanged(oldPosition);
                horizontalRecyclerviewAdapter.notifyItemChanged(CurrentPosition);
                //viewpager界面切换
                viewPager.setCurrentItem(position, false);
            }

            @Override
            public void onItemLongClick(View view, int position, List<ImageModel> datas) {
            }
        });


    }

    private void replaceFragment() {
        //创建fragment的工厂类
        FragmentFactory factory = FragmentFactory.getSingleFactoryInstance();
        //创建修改实例
        EmotionComplateFragment f1 = (EmotionComplateFragment) factory.getFragment(EmotionUtils.EMOTION_CLASSIC_TYPE);
        EmotionVerticalFragment emotionVerticalFragment = new EmotionVerticalFragment(EmotionMainFragment.etEditInput);
        fragments.add(emotionVerticalFragment);
        Bundle b = null;
//        for (int i = 0; i < 7; i++) {
//            b = new Bundle();
//            b.putString("Interge", "Fragment-" + i);
//            Fragment1 fg = Fragment1.newInstance(Fragment1.class, b);
//            fragments.add(fg);
//        }

        NoHorizontalScrollerVPAdapter adapter = new NoHorizontalScrollerVPAdapter(getChildFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
    }
}
